package com.wugui.datax.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wugui.datatx.core.biz.model.ReturnT;
import com.wugui.datax.admin.entity.JobDatasourceFile;
import com.wugui.datax.admin.mapper.JobDatasourceFileMapper;
import com.wugui.datax.admin.service.JobDatasourceFileService;
import com.wugui.datax.admin.util.FileConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/****************************************
 *
 *@Author: wangzp
 *@Date: 2023/8/1 周二 18:20
 *@Desc:
 *
 *****************************************/
@Service
public class JobDatasourceFileServiceImpl extends ServiceImpl<JobDatasourceFileMapper, JobDatasourceFile> implements JobDatasourceFileService{

    @Value("${file.store.path}")
    private String fileDirStorePath;

    @Autowired
    JobDatasourceFileMapper fileMapper;

    @Override
    public List<JobDatasourceFile> getFilesByDatasourceId(Long datasourceId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("datasource_id",datasourceId);
        return fileMapper.selectList(queryWrapper);
    }

    @Override
    public ReturnT<String> upload(MultipartFile file) throws Exception {

        //获取文件名
        String fileName = file.getOriginalFilename();

        //获取文件后缀名
        String extension = "";
        if (fileName != null && fileName.lastIndexOf(".") != -1) {
            extension = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        //获取文件类型
        String fileType = FileConstants.getFileTypeByName(extension);
        //拼接文件存储目录
        String fileStorePath = fileDirStorePath + File.separator + fileType ;

        //文件存储目录
        File fileStoreDir = new File(fileStorePath);
        if (!fileStoreDir.exists()){
            fileStoreDir.mkdirs();
        }

        String filePath = fileStorePath + File.separator + fileName;
        //判断文件是否存在
        File tmpFile = new File(filePath);
        if (tmpFile.exists()){
            return new ReturnT<>(ReturnT.FAIL_CODE,"文件已存在");
        }

        file.transferTo(tmpFile);

        JobDatasourceFile datasourceFile = new JobDatasourceFile();
        datasourceFile.setFilePath(filePath);
        datasourceFile.setFileName(fileName);

        fileMapper.insert(datasourceFile);

        return new ReturnT<>(ReturnT.SUCCESS_CODE, String.valueOf(datasourceFile.getId()));
    }

    public Boolean delByDatasourceIds(List<Long> datasourceIds){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("datasource_id",datasourceIds);
        List<JobDatasourceFile> datasourceFiles = this.fileMapper.selectList(queryWrapper);
        File file = null;
        for (JobDatasourceFile datasourceFile : datasourceFiles){
            file = new File(datasourceFile.getFilePath());
            file.deleteOnExit();
        }
        return this.fileMapper.delete(queryWrapper)>0;
    }
}
